<template>
  <div class="cate-container">
    <!-- <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>文章列表</el-breadcrumb-item>
    </el-breadcrumb> -->
    <el-card>
      <div class="cate-header">
        <Bread first="文章管理" second="文章分类"></Bread>
        <el-button @click="addCate">添加</el-button>
      </div>
      <!-- 表格 -->
      <el-table border :data="artList" style="width: 100%; margin: 20px 0">
        <el-table-column
          label="序号"
          width="180"
          type="index"
        ></el-table-column>

        <el-table-column label="分类名称" prop="cate_name"></el-table-column>

        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <pre>{{ scope.row.id }}</pre>
            <el-button type="primary" size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加分类对话框 -->

    <el-dialog
      width="40%"
      title="添加文章分类"
      :visible.sync="dialogFormVisible"
      @close="close"
    >
      <el-form ref="addCateForm" :model="form" :rules="addRules">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input v-model="form.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input v-model="form.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addHandle">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加分类对话框 -->

    <!-- 编辑 -->
    <el-dialog
      width="40%"
      title="编辑文章分类"
      :visible.sync="dialogFormVisibleEdit"
      @close="close2"
    >
      <el-form ref="EditCateForm" :model="formEdit" :rules="addRules">
        <el-form-item label="分类名称" label-width="90px" prop="cate_name">
          <el-input v-model="formEdit.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" label-width="90px" prop="cate_alias">
          <el-input v-model="formEdit.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="editHandle">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑 -->
  </div>
</template>
           
<script>
import { addCateApi, delCateByIdApi, editCateByIdApi } from '@/api/art'
import { mapState, mapActions } from 'vuex'
export default {
  name: '',
  data() {
    return {
      dialogFormVisible: false,
      form: {
        cate_name: '',
        cate_alias: '',
      },
      // 添加的表单验证
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur',
          },
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur',
          },
        ],
      },

      /* 编辑 */
      dialogFormVisibleEdit: false,
      formEdit: {
        cate_name: '',
        cate_alias: '',
      },
    }
  },
  created() {
    this.AgetArtList()
  },
  computed: {
    ...mapState('article', ['artList']),
  },
  methods: {
    ...mapActions('article', ['AgetArtList']),
    editHandle() {
      //  点击编辑对话框中的确认按钮
      //  3、预校验
      this.$refs.EditCateForm.validate(async (valid) => {
        if (valid) {
          //  4、发送请求
          const res = await editCateByIdApi(this.formEdit)
          console.log(res, 88)
          // 5、刷新页面+关闭对话框
          if (res.data.code === 0) {
            this.dialogFormVisibleEdit = false
            this.AgetArtList()
          }
        }
      })
    },
    handleEdit(row) {
      /* 点击的是编辑按钮 */
      /* 
      1、显示对话框
      2、表单显示当前点击行的数据
      3、校验+预校验
      4、发送请求
      5、刷新页面+关闭对话框
       */

      //   1-显示对话框
      this.dialogFormVisibleEdit = true
      console.log(row, 6)
      //  2-表单显示当前点击行的数据
      this.formEdit = row
    },
    handleDelete(id) {
      /* 
      根据id删除文章分类
      + 显示提示框
      + 发送请求
      + 更新页面
       */
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          /* 2-发送请求 */
          const res = await delCateByIdApi(id)
          console.log(res, 888)
          /* 3-更新页面 */
          if (res.data.code === 0) {
            //  删除成功
            this.AgetArtList()
          }
          /* 点击的确认按钮 */
          this.$message({
            type: 'success',
            message: '删除成功!',
          })
        })
        .catch(() => {
          /* 点击的 取消按钮 */
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    /* 添加分类 */
    addCate() {
      /* 
      1、显示对话框
      2、成功后数据列表更新
       */
      this.dialogFormVisible = true
    },
    close() {
      /* 添加的重置 */
      /* 重置表单 */
      this.$refs['addCateForm'].resetFields()
    },
    close2() {
      /* 编辑重置表单 */
      this.$refs['EditCateForm'].resetFields()
    },
    /* 点击对话框中的确认按钮 */
    addHandle() {
      /* 
      1、预校验
      2、发送请求
      3、刷新页面
      4-关闭对话框
       */

      //  1、预校验
      this.$refs.addCateForm.validate(async (valid) => {
        if (valid) {
          //  校验成功
          //2、发送请求 api
          const res = await addCateApi(this.form)
          console.log(res, 888)
          if (res.data.code === 0) {
            this.$message({
              message: '恭喜你，添加分类成功',
              type: 'success',
            })
            //  表示添加成功
            //  3、刷新页面
            this.AgetArtList()
            // 4-关闭对话框
            this.dialogFormVisible = false
          }
        }
      })
    },
  },
}
</script>
<style lang='less'  scoped>
.cate-container {
  .cate-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>